<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { reactive, watch, inject, Ref, onBeforeUnmount } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { Row } from '@/api/franchiser'
const { required } = useValidator()
const currentRow: Ref<Row> = inject('currentRow') as Ref<Row>

const { formRegister, formMethods } = useForm()

const { setValues, getFormData, getElFormExpose } = formMethods

const schema = reactive<FormSchema[]>([
  {
    field: '',
    label: '公司信息',
    component: 'Divider',
    colProps: { span: 28 },
    componentProps: {
      borderStyle: 'none',
      contentPosition: 'left'
    }
  },
  {
    field: 'name',
    label: '客户名称',
    component: 'Input',
    formItemProps: {
      rules: [required(), { min: 1, max: 50, message: '最多输入50个字符', trigger: 'blur' }]
    },
    componentProps: {
      placeholder: '最多50个字符'
    }
  },
  {
    field: 'address',
    label: '经营地址',
    component: 'Input',
    formItemProps: {
      rules: [required(), { min: 1, max: 50, message: '最多输入50个字符', trigger: 'blur' }]
    },
    componentProps: {
      placeholder: '最多50个字符'
    }
  },
  {
    field: 'remark',
    label: '备注信息',
    component: 'Input',
    formItemProps: {
      rules: [{ min: 1, max: 50, message: '请输入最多支持50字符', trigger: 'blur' }]
    }
  },
  {
    field: '',
    label: '个人信息',
    component: 'Divider',
    colProps: { span: 28 },
    componentProps: {
      borderStyle: 'none',
      contentPosition: 'left'
    }
  },
  {
    field: 'real_name',
    label: '联系人',
    component: 'Input',
    formItemProps: {
      rules: [required(), { min: 1, max: 50, message: '请输入最多支持50字符', trigger: 'blur' }]
    }
  },
  {
    field: 'mobile',
    label: '联系方式',
    component: 'Input',
    formItemProps: {
      rules: [
        required(),
        { min: 11, max: 11, message: '请输入负责人联系方式', trigger: 'blur' },
        {
          pattern: /^[1][3,4,5,6,7,8,9][0-9]{9}$/,
          message: '请输入负责人联系方式',
          trigger: 'blur'
        }
      ]
    }
  },
  {
    field: '',
    label: '账号信息',
    component: 'Divider',
    colProps: { span: 28 },
    componentProps: {
      borderStyle: 'none',
      contentPosition: 'left'
    }
  },
  {
    field: 'user_name',
    label: '登录账号',
    component: 'Input',
    formItemProps: {
      rules: [
        required(),
        {
          min: 6,
          max: 20,
          message: '请输入(至少6位,至多20位,不允许与当前账号重复)',
          trigger: 'blur'
        }
      ]
    },
    componentProps: {
      placeholder: '请输入(至少6位,至多20位,不允许与当前账号重复)'
    }
  },
  {
    field: 'password',
    label: '登录密码',
    component: 'InputPassword',
    formItemProps: {
      rules: [{ min: 6, max: 10, message: '请输入六到十位密码', trigger: 'blur' }]
    },
    componentProps: {
      placeholder: '请输入六到十位密码'
    }
  },
  {
    field: 'passworded',
    label: '确定密码',
    component: 'InputPassword',
    formItemProps: {
      rules: [
        {
          asyncValidator: async (_, value, callback) => {
            const formData = await getFormData()
            if (value !== formData.password) {
              callback(new Error('两次输入的新密码不一致'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }
      ]
    },
    componentProps: {
      placeholder: '请输入确定密码'
    }
  }
])

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const formData = await getFormData()
    const formDataList = {
      id: formData.id || '',
      name: formData.name || '',
      real_name: formData.real_name || '',
      password: formData.password || '',
      mobile: formData.mobile || '',
      user_name: formData.user_name || '',
      address: formData.address || '',
      remark: formData.remark || ''
    }
    return formDataList
  }
}

const reset = async () => {
  const elForm = await getElFormExpose()
  elForm?.resetFields()
}

watch(
  () => currentRow.value,
  (currentRow) => {
    if (!currentRow) return
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

onBeforeUnmount(reset)

defineExpose({
  submit,
  reset
})
</script>

<template>
  <Form @register="formRegister" :schema="schema" />
</template>
